package com.cards_game.simple_ddz.component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import com.cards_game.simple_ddz.bean.PlayerBean;
import com.cards_game.simple_ddz.bean.SimpleDdzBean;
import com.cards_game.simple_ddz.common.GameProgressEnum;
import com.cards_game.simple_ddz.common.RoomBean;
import com.cards_game.simple_ddz.constants.CardConstants;
import com.cards_game.simple_ddz.verify.SimpleDdzVerify;

/**
 * @author 郭国辉
 * 
 *     2017年7月14日下午10:12:04
 */
public class SimpleDdzCardsUtils {
	public static void main(String[] args) {
		List<SimpleDdzBean> l=getSimpleDdzCards();
		System.out.println(l);
		
	}
	
	public static PlayerBean cardsToPlayers(int userId, RoomBean rb) {
		Vector<PlayerBean> players = rb.getPlayerList();
		PlayerBean pb = null;
		for (PlayerBean _pb : players) {
			if (_pb.getId() == userId) {
				pb = _pb;
			}
		}
		if(rb.getGpe()==GameProgressEnum.ALL_READY){
			synchronized (rb) {
				if(rb.getGpe()==GameProgressEnum.ALL_READY){
					for(PlayerBean _pb:players){
						_pb.getCards().clear();
					}
					rb.setGpe(GameProgressEnum.GRAB_LANDLORD);
					List<SimpleDdzBean> list = rb.getOriginalCards();
					// 洗牌
					Collections.shuffle(list);
					for (int i = 0; i < list.size() - 3; i++) {
						players.get(i % 3).getCards().add(list.get(i));
					}
					descCards(pb.getCards());
				}
			}
		}
		return pb;
	}
	/**
	 * 获取一副原始纸牌
	 * @return
	 */
	public static List<SimpleDdzBean> getSimpleDdzCards() {
		List<SimpleDdzBean> list = new ArrayList<>();
		for (CardConstants.Number num : CardConstants.Number.values()) {
			for (CardConstants.Suits suit : CardConstants.Suits.values()) {
				SimpleDdzBean sdb = new SimpleDdzBean();
				sdb.setNum(num.getValue());
				sdb.setSuit(suit);
				list.add(sdb);
			}
		}
		SimpleDdzBean MaxKing = new SimpleDdzBean();
		MaxKing.setJoker(CardConstants.Joker.Max);
		list.add(MaxKing);
		SimpleDdzBean MinKing = new SimpleDdzBean();
		MinKing.setJoker(CardConstants.Joker.Min);
		list.add(MinKing);
		
		return list;
	}
	/**
	 * 倒叙排列当前纸牌
	 * @param cards
	 * @return
	 */
	public  static List<SimpleDdzBean> descCards(List<SimpleDdzBean> cards){
		Comparator<SimpleDdzBean> sort=new Comparator<SimpleDdzBean>() {
			@Override
			public int compare(SimpleDdzBean o1, SimpleDdzBean o2) {
				int before=SimpleDdzVerify.getSimpleDdzVerify().getWeight(o1.getNum());
				int after=SimpleDdzVerify.getSimpleDdzVerify().getWeight(o2.getNum());
				if(before==after){
					if(before==14){
						return o1.getJoker().ordinal() -o2.getJoker().ordinal();
					}else{
						return o1.getSuit().ordinal() -o2.getSuit().ordinal();
					}
				}
				return after-before;
			}
		};
		cards.sort(sort);
//		ComparatorUtils.
		return cards;
	}
}
